<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  User: 黄星
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/static/h-ui.admin/js/H-ui.admin.js"></script>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/common.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/question-update.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/page/admin/static/h-ui.admin/skin/default/skin.css" id="skin" />

    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/utils.js"></script>

    <script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/layer/2.4/layer.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/static/h-ui/js/H-ui.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/jquery.contextmenu/jquery.contextmenu.r2.js"></script>

    <script type="text/javascript">
//初始化函数
        $(function () {
            majorSelect();
            classesSelect();
        });

        //专业下拉框发生变化
        function majorSelect() {
            $("#majorSelect").change(function () {
                var majorId = $("#majorSelect option:selected").val();
                var gradeId = 0;
                $.ajax({
                    url: "${pageContext.request.contextPath}/QuestionManage/getSubjectToUpdate",
                    type: "get",
                    dataType: "json",
                    data: {"majorId": majorId, "gradeId": gradeId},
                    async: false,
                    success: function (paging) {
                        $("#subjectSelect").empty();
                        $("#subjectSelect").append(
                            '<option value=0>请选择科目</option>'
                        );
                        $.each(paging.list, function (index, subjectList) {
                            $("#subjectSelect").append(
                                '<option value=' + subjectList.id + '> ' + subjectList.subjectName + '</option>'
                            );
                        });
                        $("#subjectSelect").val(0);
                        $("#classesSelect").val(0);
                    },
                    error: function () {
                        alert('error');
                    }
                });
            })
        }

        //班级下拉框发生变化
        function classesSelect() {
            $("#classesSelect").change(function () {
                var majorId = $("#majorSelect option:selected").val();
                var gradeId = $("#classesSelect option:selected").val();
                $.ajax({
                    url: "${pageContext.request.contextPath}/QuestionManage/getSubjectToUpdate",
                    type: "get",
                    dataType: "json",
                    data: {"majorId": majorId, "gradeId": gradeId},
                    async: false,
                    success: function (paging) {
                        $("#subjectSelect").empty();
                        $("#subjectSelect").append(
                            '<option value=0>请选择科目</option>'
                        );
                        $.each(paging.list, function (index, subjectList) {
                            $("#subjectSelect").append(
                                '<option value=' + subjectList.id + '> ' + subjectList.subjectName + '</option>'
                            );
                        });
                        $("#subjectSelect").val(0);
                    },
                    error: function () {
                        alert('error');
                    }
                });
            })
        }

        //确定点击
        function updateClick() {
            var typeId = ${question.type.id};
            var questionId = ${question.id};
            var subjectId = $("#subjectSelect").val();
            var submit = false;
            //不同的题目获取不同的信息
            var data = "questionId=" + questionId + "&typeId=" + typeId + "&subjectId=" + subjectId;
            if(typeId == 1){
                //选择题
                var ErrorAnswer1 =  $("#ErrorAnswer1").val();
                var ErrorAnswer2 =  $("#ErrorAnswer2").val();
                var ErrorAnswer3 =  $("#ErrorAnswer3").val();

                data += "&topic=" + $("#choiceTopic").val();
                data += "&answer=" + $("#choiceAnswer").val();
                data += "&errAnswer=" +ErrorAnswer1 + "~" + ErrorAnswer2 + "~" + ErrorAnswer3;
                if(!errAnswerIsSame(ErrorAnswer1,ErrorAnswer2,ErrorAnswer3)){
                    submit = true;
                }else{
                    alert("三个答案不能相同");
                }
            }else if(typeId == 2){
                //填空题
                data += "&topic=" + $("#blankTopic1").val() + "_" + $("#blankTopic2").val();
                data += "&answer=" + $("#blankAnswer").val();
           }else if(typeId == 3){
                //判断题
                data += "&topic=" + $("#judgeTopic").val();
                data += "&errAnswer=" + $("#judgeErrAnswer").val();
                if(!isEmptyString($("#judgeTopic").val()) && !isEmptyString($("#judgeErrAnswer").val()) ){
                    submit = true;
                }else {
                    alert("两个题目都不能为空")
                }
            }
            if(submit){
                $.ajax({
                    url: "${pageContext.request.contextPath}/QuestionManage/updQuestion",
                    type: "get",
                    dataType: "text",
                    data: data,
                    async: false,
                    success: function (message) {
                        parent.layer.msg(message,{icon:1,time:2000});
                        parent.document.getElementById("updFlash").click();
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    },
                    error: function (message) {
                        parent.layer.msg(message,{icon:1,time:2000});
                    }
                });
            }


        }

        /*判断选择题三个错误答案是否一样*/
        function errAnswerIsSame(err1,err2,err3) {

            if(err1.trim() == err2.trim() || err1.trim() == err3.trim() || err2.trim() == err3.trim()){
                return true;
            }
            else {
                return false;
            }
        }

        //判断是否为空串  是返回true 否则 false
        function isEmptyString(str) {
            if (str == "") {
                return true;
            } else {
                return false;
            }
        }

        //弹窗关闭
        function layerClose() {
            layer_close();
        }

    </script>
</head>
<body>

<form id="question"  autocomplete="off">
    <div class="clearfix question-upd-main">
        <%--显示题目编号--%>
        <div class="left question-upd-questionid">
            题目编号：${question.id}
        </div>
        <%--显示专业名称下拉框--%>
        <div class="left question-upd-major">
            专业名称：
            <select id="majorSelect">
                <c:forEach items="${majorList}" var="m">
                    <option value="${m.id}"
                            <c:if test="${m.id == question.subject.majorId}">selected</c:if>>${m.majorName}</option>
                </c:forEach>
            </select>
        </div>
        <%--显示年级--%>
        <div class="left question-upd-major">
            所属年级：
            <select id="classesSelect">
                <option value="0" <c:if test="${gradeId == 0}"> selected </c:if>>请先选择年级</option>
                <option value="1" <c:if test="${gradeId == 1}"> selected </c:if>>大一</option>
                <option value="2" <c:if test="${gradeId == 2}"> selected </c:if>>大二</option>
                <option value="3" <c:if test="${gradeId == 3}"> selected </c:if>>大三</option>
                <option value="4" <c:if test="${gradeId == 4}"> selected </c:if>>大四</option>
            </select>
        </div>
        <%--显示该年级所有科目--%>
        <div class="left question-upd-major">
            所属科目：
            <select id="subjectSelect">
                <c:forEach items="${subjectList}" var="s">
                    <option value="${s.id}" <c:if
                            test="${s.id == question.subject.id}"> selected </c:if>>${s.subjectName}</option>
                </c:forEach>
            </select>
        </div>
        <%--显示题目类型--%>
        <div class="left question-upd-major">
            题目类型：${question.type.typeName}
        </div>
        <%--判断题目类型 来显示对应的修改页面--%>
        <%--选择题--%>
        <c:if test="${question.type.id == 1}">
            <div class="left question-upd-choice">
                题目：<br/>
                <input id="choiceTopic" type="text" value="${question.topic}">
            </div>
            <div class="left question-upd-choice">
                正确答案：<br/>
                <input id="choiceAnswer" type="text" value="${question.answer}">
            </div>
            <div class="left question-upd-choice">
                错误答案：<br/>
                <c:forEach items="${split}" var="sp" varStatus="vs">
                    <input id="ErrorAnswer${vs.count}" type="text" value="${sp}">
                </c:forEach>
            </div>

        </c:if>
        <%--填空题--%>
        <c:if test="${question.type.id == 2}">
            <div class="left question-upd-blank">
                <c:forEach items="${split}" var="sp" varStatus="vs">
                    <c:if test="${vs.count == 1}">空前题目：<br/></c:if>
                    <c:if test="${vs.count == 2}"><br/>空后题目：<br/></c:if>
                    <input id="blankTopic${vs.count}" type="text" value="${sp}"><br/>
                </c:forEach>
            </div>
            <div class="left question-upd-blank">
                正确答案：<br/>
                <input id="blankAnswer" type="text" value="${question.answer}"><br/>
            </div>
        </c:if>
        <%--判断题--%>
        <c:if test="${question.type.id == 3}">
            <div class="left question-upd-judge">
                正确题目：<br/>
                <input id="judgeTopic" type="text" value="${question.topic}"><br/><br/>
                错误题目：<br/>
                <input id="judgeErrAnswer" type="text" value="${question.errAnswer}"><br/>
            </div>
        </c:if>

            <div class="left" style="width: 100%;margin-top: 5%;" align="center">
                <input type="button" class="question-upd-btn" onclick="updateClick()" value="确定"/>
                <input type="button" class="question-upd-btn" onclick="layerClose()" value="关闭" style="margin-left: 15%;"/>
            </div>
    </div>

</form>
</body>
</html>
